import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookie from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: '/main',
        name: 'Main',
        component: () => import('../views/SecondLevel/Main.vue')
      },
      {
        path: '/apartmentHunting',
        name: 'ApartmentHunting',
        component: () => import('../views/SecondLevel/ApartmentHunting.vue')
      },{
        path: '/issue',
        name: 'Issue',
        component: () => import('../views/SecondLevel/Issue.vue')
      },{
        path: '/message',
        name: 'Message',
        component: () => import('../views/SecondLevel/Message.vue')
      },{
        path: '/My',
        name: 'My',
        component: () => import('../views/SecondLevel/My.vue'),
        // 路由独享守卫
        beforeEnter: (to, from, next) => {
          if(Cookie.get('token')) {
            next()
          } else {
            next('/login?toPath=/my')
          }
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


export default router
